package com.example.chapter8iservice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private boolean bound = true;
    MyService service;
    private Button btnstop,btnresume,btnpause;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn_bind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, MyService.class);
                bindService(intent,sc, Context.BIND_AUTO_CREATE);
                bound = true;
                btnpause.setEnabled(true);
//                btnresume.setEnabled(false);
                btnstop.setEnabled(true);
            }
        });
        findViewById(R.id.btn_unbind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bound){
                    unbindService(sc);
                    bound = false;
                    btnpause.setEnabled(false);
                    btnresume.setEnabled(false);
                    btnstop.setEnabled(false);
                }
            }
        });
        btnpause = findViewById(R.id.btn_pause_play);
        btnpause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                service.pause();
                btnpause.setEnabled(false);
                btnresume.setEnabled(true);
                btnstop.setEnabled(true);
            }
        });
        btnresume = findViewById(R.id.btn_resume_play);
        btnresume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                service.pause();
                btnpause.setEnabled(true);
                btnresume.setEnabled(false);
                btnstop.setEnabled(true);
            }
        });
        btnstop = findViewById(R.id.btn_stop_play);
        btnstop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                service.pause();
                btnpause.setEnabled(false);
                btnresume.setEnabled(true);
                btnstop.setEnabled(false);
            }
        });
    }
    private ServiceConnection sc = new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            // IBinder 得到Service的对象
            MyService.MusicBinder musicBinder = (MyService.MusicBinder) iBinder;
            service = musicBinder.getService();
            bound = true;
            Toast.makeText(MainActivity.this,"服务被绑定",Toast.LENGTH_LONG).show();

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            bound = false;
            Toast.makeText(MainActivity.this,"服务链接失败",Toast.LENGTH_LONG).show();
        }
    };
}